package com.mlnx.ui.service.impl;

import com.mlnx.service.base.service.BaseService;
import com.mlnx.service.common.enums.ResponseEnum;
import com.mlnx.service.common.exception.MutilDeviceServiceException;
import com.mlnx.ui.dao.SportTrainMapper;
import com.mlnx.ui.dao.SportWayMapper;
import com.mlnx.ui.entity.SportTrain;
import com.mlnx.ui.entity.SportWay;
import com.mlnx.ui.pojo.vo.SportWayVO;
import com.mlnx.ui.service.SportWayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class SportWayServiceImpl extends BaseService<SportWayMapper, SportWay> implements SportWayService {

    @Autowired
    @Override
    public void setBaseMapper(SportWayMapper baseMapper) {
        super.setBaseMapper(baseMapper);
    }

    @Autowired
    private SportTrainMapper sportTrainMapper;

    @Override
    public List<SportWayVO> getSportPrescriptions(Integer patientId) {
        return baseMapper.selectSportPrescriptions(patientId);
    }

    @Override
    @Transactional
    public void saveSportPrescription(SportWayVO sportWayVO) {
        sportWayVO.getSportWay().setSubmitTime(new Date());
        baseMapper.insert(sportWayVO.getSportWay());

        if (sportWayVO.getSportTrainList() != null && sportWayVO.getSportTrainList().size() > 0) {
            for (SportTrain sportTrain : sportWayVO.getSportTrainList()) {
                sportTrain.setWayId(sportWayVO.getSportWay().getId());
                try {
                    sportTrainMapper.insert(sportTrain);
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new MutilDeviceServiceException(ResponseEnum.ServerError);
                }
            }
        }
    }
}
